/**
 * Created by jiachneyu on 2018/4/21.
 */
const articleModel = require("../model/blog/article")
const getArticle = articleModel.getArticle;
const addIp = articleModel.addIp;
const likeModel = articleModel.like;
const addDiscussModel = articleModel.addDiscuss;
const getDiscussModel = articleModel.getDiscuss;
const replayDiscussModel = articleModel.replayDiscuss;
const getArticleName = articleModel.getArticleName;
const getReplyEmail = articleModel.getReplyEmail;
const {sendEmail} = require("../common/email");
const article = async (ctx, next) => {
    await getArticle(ctx.query.id, ctx.query.pv).then(res => {
        let returnJson = {};
        if (res.code === 1) {
            returnJson = {
                ...res,
                msg: "查询成功"
            }
        }
        else {
            returnJson = {
                code: -1,
                msg: "查询失败，请重试"
            }
        }
        ctx.body = returnJson;
    });
    // if(ctx.query.pv == 0 || ctx.query.pv == 1){
    //     await addIp(ctx.query.id, getIp())
    // }
}

const like = async (ctx) => {
    await likeModel(ctx.request.body).then(res => {
        let returnJson = {};
        if (res.code === 1) {
            returnJson = {
                ...res,
                msg: "点赞成功"
            }
        }
        else {
            returnJson = {
                code: -1,
                msg: "点赞失败，请重试"
            }
        }
        ctx.body = returnJson;
    })
}

const addDiscuss = async (ctx) => {
    const body = ctx.request.body;
    body.isChecked = 0;
    if (body.toName) {
        await replayDiscussModel(body).then(async res => {
            let returnJson = {};
            if (res.code === 1) {
                let email = "";
                await getReplyEmail(body).then(res => {
                    console.log("email111:", res)
                    if (res.code === 1) {
                        email = res.data.email
                    }
                });
                // sendEmail({
                //     subject: `${title}文章评论回复邮件`,
                //     to: email,
                //     html: `<p>你在"${title}"文章中的评论有人回复了，回复内容："${body.content}",地址为<a href="http://www.jiachenyu.cn/blog/article/${body.blog}">http://www.jiachenyu.cn/blog/article/${body.blog}</a>`
                // })
                returnJson = {
                    ...res,
                    msg: "评论成功"
                }
            }
            else {
                returnJson = {
                    code: -1,
                    msg: "评论失败，请重试"
                }
            }
            ctx.body = returnJson;
        })
    }
    else {
        let title = "";
        await getArticleName(body.blog).then(res => {
            if (res.code === 1) {
                title = res.data.title
            }
        });
        await addDiscussModel(body).then(res => {
            let returnJson = {};
            if (res.code === 1) {
                sendEmail({
                    subject: `${title}文章评论邮件`,
                    to: "1539981050@qq.com",
                    html: `<p>有人在你的"${title}"文章中评论，评论内容："${body.content}",地址为<a href="http://www.jiachenyu.cn/blog/article/${body.blog}">http://www.jiachenyu.cn/blog/article/${body.blog}</a>`
                })
                returnJson = {
                    ...res,
                    msg: "评论成功"
                }
            }
            else {
                returnJson = {
                    code: -1,
                    msg: "评论失败，请重试"
                }
            }
            ctx.body = returnJson;
        })
    }
}

const getDiscuss = async (ctx) => {
    await getDiscussModel(ctx.query.id).then(res => {
        let returnJson = {};
        if (res.code === 1) {
            returnJson = {
                ...res,
                msg: "获取评论成功"
            }
        }
        else {
            returnJson = {
                code: -1,
                msg: "获取评论失败，请重试"
            }
        }
        ctx.body = returnJson;
    })
}

const getIp = (req) => {
    return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
}

module.exports = [
    {
        method: "get",
        path: "/api/blog/article",
        handle: article
    },
    {
        method: "post",
        path: "/api/blog/like",
        handle: like
    },
    {
        method: "post",
        path: "/api/blog/addDiscuss",
        handle: addDiscuss
    },
    {
        method: "get",
        path: "/api/blog/getDiscuss",
        handle: getDiscuss
    },
]